{#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#}

#
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
#

{# TODO: Look for { or } in created file #}
# NAGIOS SERVER Check (status log update)
{% if hostgroup_defs['nagios-server'] %}
define service {
        name                            hadoop-service
        use                             generic-service
        notification_options            w,u,c,r,f,s
        first_notification_delay        0
        notification_interval           0                 # Send the notification once
        contact_groups                  admins
        notifications_enabled           1
        event_handler_enabled           1
        register                        0
}

define service {        
        hostgroup_name          nagios-server        
        use                     hadoop-service
        service_description     NAGIOS::Nagios status log freshness
        servicegroups           NAGIOS
        check_command           check_nagios!10!/var/nagios/status.dat!{{nagios_lookup_daemon_str}}
        normal_check_interval   5
        retry_check_interval    0.5
        max_check_attempts      2
}

# NAGIOS SERVER HDFS Checks
{% if hostgroup_defs['namenode'] %}
define service {
        hostgroup_name          nagios-server
        use                     hadoop-service
        service_description     HDFS::Percent DataNodes with space available
        servicegroups           HDFS
        check_command           check_aggregate!"DATANODE::DataNode space"!10%!30%
        normal_check_interval   0.5
        retry_check_interval    1 
        max_check_attempts      1
}

define service {
        hostgroup_name          nagios-server
        use                     hadoop-service
        service_description     HDFS::Percent DataNodes live
        servicegroups           HDFS
        check_command           check_aggregate!"DATANODE::DataNode process"!10%!30%
        normal_check_interval   0.5
        retry_check_interval    0.25
        max_check_attempts      3
}
{% endif %}

# AMBARI AGENT Checks
{% for hostname in all_hosts %}
define service {
        host_name	        {{ hostname }}
        use                     hadoop-service
        service_description     AMBARI::Ambari Agent process
        servicegroups           AMBARI
        check_command           check_tcp_wrapper!{{all_ping_ports[loop.index-1]}}!-w 1 -c 1
        normal_check_interval   1
        retry_check_interval    0.25
        max_check_attempts      4
}

{% endfor %}

# NAGIOS SERVER ZOOKEEPER Checks
{% if hostgroup_defs['zookeeper-servers'] %}
define service {
        hostgroup_name          nagios-server
        use                     hadoop-service
        service_description     ZOOKEEPER::Percent ZooKeeper Servers live
        servicegroups           ZOOKEEPER
        check_command           check_aggregate!"ZOOKEEPER::ZooKeeper Server process"!35%!70%
        normal_check_interval   0.5
        retry_check_interval    0.25
        max_check_attempts      3
}
{% endif %}

# NAGIOS SERVER HBASE Checks
{% if hostgroup_defs['hbasemasters'] %}
define service {
        hostgroup_name          nagios-server
        use                     hadoop-service
        service_description     HBASE::Percent RegionServers live
        servicegroups           HBASE
        check_command           check_aggregate!"REGIONSERVER::RegionServer process"!10%!30%
        normal_check_interval   0.5
        retry_check_interval    0.25
        max_check_attempts      3
}
{% endif %}
{% endif %}

# GANGLIA SERVER Checks
{% if hostgroup_defs['ganglia-server'] %}
define service {
        hostgroup_name          ganglia-server
        use                     hadoop-service
        service_description     GANGLIA::Ganglia Server process
        servicegroups           GANGLIA
        _host_component         GANGLIA_SERVER
        check_command           check_tcp_wrapper!{{ ganglia_port }}!-w 1 -c 1
        normal_check_interval   0.25
        retry_check_interval    0.25
        max_check_attempts      4
}

{% if hostgroup_defs['namenode'] %}
{% for hostname in hostgroup_defs['namenode'] %}
define service {
        host_name	        {{ hostname }}
        use                     hadoop-service
        service_description     GANGLIA::Ganglia Monitor process for NameNode
        servicegroups           GANGLIA
        _host_component         GANGLIA_MONITOR
        check_command           check_tcp_wrapper!{{ ganglia_collector_namenode_port }}!-w 1 -c 1
        normal_check_interval   0.25
        retry_check_interval    0.25
        max_check_attempts      4
}
{% endfor %}
{% endif %}

{% if hostgroup_defs['jobtracker'] %}
{% for hostname in hostgroup_defs['jobtracker'] %}
define service {
        host_name	        {{ hostname }}
        use                     hadoop-service
        service_description     GANGLIA::Ganglia Monitor process for JobTracker
        servicegroups           GANGLIA
        _host_component         GANGLIA_MONITOR
        check_command           check_tcp_wrapper!{{ ganglia_collector_jobtracker_port }}!-w 1 -c 1
        normal_check_interval   0.25
        retry_check_interval    0.25
        max_check_attempts      4
}
{% endfor %}
{% endif %}

{% if hostgroup_defs['hbasemasters'] %}
{% for hostname in hostgroup_defs['hbasemasters'] %}
define service {
        host_name	        {{ hostname }}
        use                     hadoop-service
        service_description     GANGLIA::Ganglia Monitor process for HBase Master
        servicegroups           GANGLIA
        _host_component         GANGLIA_MONITOR
        check_command           check_tcp_wrapper!{{ ganglia_collector_hbase_port }}!-w 1 -c 1
        normal_check_interval   0.25
        retry_check_interval    0.25
        max_check_attempts      4
}
{% endfor %}
{% endif %}


{% if hostgroup_defs['historyserver2'] %}
{% for hostname in hostgroup_defs['historyserver2'] %}
define service {
        host_name	        {{ hostname }}
        use                     hadoop-service
        service_description     GANGLIA::Ganglia Monitor process for HistoryServer
        servicegroups           GANGLIA
        _host_component         GANGLIA_MONITOR
        check_command           check_tcp_wrapper!{{ ganglia_collector_hs_port }}!-w 1 -c 1
        normal_check_interval   0.25
        retry_check_interval    0.25
        max_check_attempts      4
}
{% endfor %}
{% endif %}

{% endif %}

{% if hostgroup_defs['snamenode'] %}
# Secondary namenode checks
define service {
        hostgroup_name          snamenode
        use                     hadoop-service
        service_description     NAMENODE::Secondary NameNode process
        servicegroups           HDFS
        check_command           check_tcp_wrapper!{{ snamenode_port }}!-w 1 -c 1
        _host_component         SECONDARY_NAMENODE
        normal_check_interval   0.5
        retry_check_interval    0.25
        max_check_attempts      3
}
{% endif %}


{% if hostgroup_defs['namenode'] %}
# HDFS Checks
{%  for namenode_hostname in namenode_host %}
{# TODO: check if we can get rid of str, lower #}
define service {
        host_name               {{ namenode_hostname }}
        use                     hadoop-service
        service_description     NAMENODE::NameNode edit logs directory status on {{ namenode_hostname }}
        servicegroups           HDFS
        check_command           check_name_dir_status!{{ namenode_port }}!{{ str(hadoop_ssl_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}!{{ str(security_enabled).lower() }}
        _host_component         NAMENODE
        normal_check_interval   0.5
        retry_check_interval    0.5
        max_check_attempts      3
}

{% if check_cpu_on %}
define service {
        host_name               {{ namenode_hostname }}
        use                     hadoop-service
        service_description     NAMENODE::NameNode host CPU utilization on {{ namenode_hostname }}
        servicegroups           HDFS
#        check_command           check_cpu!200%!250%
        check_command           check_cpu!{{ namenode_port }}!200%!250%!{{ str(hadoop_ssl_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}!{{ str(security_enabled).lower() }}
        _host_component         NAMENODE
        normal_check_interval   5
        retry_check_interval    2
        max_check_attempts      5
}
{% endif %}

define service {
        host_name               {{ namenode_hostname }}
        use                     hadoop-service
        service_description     NAMENODE::NameNode Web UI on {{ namenode_hostname }}
        servicegroups           HDFS
        check_command           check_webui!namenode!{{ namenode_port }}
        _host_component         NAMENODE
        normal_check_interval   1
        retry_check_interval    1
        max_check_attempts      3
}

define service {
        host_name               {{ namenode_hostname }}
        use                     hadoop-service
        service_description     NAMENODE::NameNode process on {{ namenode_hostname }}
        servicegroups           HDFS
        check_command           check_tcp_wrapper!{{ namenode_metadata_port }}!-w 1 -c 1
        _host_component         NAMENODE
        normal_check_interval   0.5
        retry_check_interval    0.25
        max_check_attempts      3
}

define service {
        host_name               {{ namenode_hostname }}
        use                     hadoop-service
        service_description     HDFS::NameNode RPC latency on {{ namenode_hostname }}
        servicegroups           HDFS
        check_command           check_rpcq_latency!NameNode!{{ namenode_port }}!3000!5000!{{ str(hadoop_ssl_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}!{{ str(security_enabled).lower() }}
        _host_component         NAMENODE
        normal_check_interval   5
        retry_check_interval    1
        max_check_attempts      5
}

{%  endfor  %}

define service {
        hostgroup_name          nagios-server
        use                     hadoop-service
        service_description     HDFS::Blocks health
        servicegroups           HDFS
        check_command           check_hdfs_blocks!$HOSTGROUPMEMBERS:namenode$!{{ namenode_port }}!{{ nn_metrics_property }}!{{ str(hadoop_ssl_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}!{{ str(security_enabled).lower() }}
        _host_component         DATANODE
        normal_check_interval   2
        retry_check_interval    1 
        max_check_attempts      1
}

define service {
        hostgroup_name          nagios-server
        use                     hadoop-service
        service_description     HDFS::HDFS capacity utilization
        servicegroups           HDFS
        check_command           check_hdfs_capacity!$HOSTGROUPMEMBERS:namenode$!{{ namenode_port }}!80%!90%!{{ str(hadoop_ssl_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}!{{ str(security_enabled).lower() }}
        _host_component         DATANODE
        normal_check_interval   2
        retry_check_interval    1 
        max_check_attempts      1
}

{% endif %}

# MAPREDUCE Checks
{% if hostgroup_defs['jobtracker'] %}
define service {
        hostgroup_name          jobtracker
        use                     hadoop-service
        service_description     JOBTRACKER::JobTracker Web UI
        servicegroups           MAPREDUCE
        check_command           check_webui!jobtracker!{{ jtnode_port }}
        _host_component         JOBTRACKER
        normal_check_interval   1
        retry_check_interval    1
        max_check_attempts      3
}

define service {
        hostgroup_name          jobhistory
        use                     hadoop-service
        service_description     JOBTRACKER::HistoryServer Web UI
        servicegroups           MAPREDUCE
        check_command           check_webui!jobhistory!{{ jobhistory_port }}
        _host_component         HISTORYSERVER
        normal_check_interval   1
        retry_check_interval    1
        max_check_attempts      3
}
{% if check_cpu_on %}
define service {
        hostgroup_name          jobtracker
        use                     hadoop-service
        service_description     JOBTRACKER::JobTracker CPU utilization
        servicegroups           MAPREDUCE
        check_command           check_cpu!{{ jtnode_port }}!200%!250%!{{ str(hadoop_ssl_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}!{{ str(security_enabled).lower() }}
        _host_component         JOBTRACKER
        normal_check_interval   5
        retry_check_interval    2 
        max_check_attempts      5
}
{% endif %}

define service {
        hostgroup_name          jobtracker
        use                     hadoop-service
        use                     hadoop-service
        service_description     JOBTRACKER::JobTracker process
        servicegroups           MAPREDUCE
        check_command           check_tcp_wrapper!{{ jtnode_port }}!-w 1 -c 1
        _host_component         JOBTRACKER
        normal_check_interval   0.5
        retry_check_interval    0.25
        max_check_attempts      4
}

define service {
        hostgroup_name          jobtracker
        use                     hadoop-service
        service_description     MAPREDUCE::JobTracker RPC latency
        servicegroups           MAPREDUCE
        check_command           check_rpcq_latency!JobTracker!{{ jtnode_port }}!3000!5000!{{ str(hadoop_ssl_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}!{{ str(security_enabled).lower() }}
        _host_component         JOBTRACKER
        normal_check_interval   5
        retry_check_interval    1 
        max_check_attempts      5
}

{% if hostgroup_defs['tasktracker-servers'] %}
define service {
        hostgroup_name          nagios-server
        use                     hadoop-service
        service_description     MAPREDUCE::Percent TaskTrackers live
        servicegroups           MAPREDUCE
        check_command           check_aggregate!"TASKTRACKER::TaskTracker process"!10%!30%
        normal_check_interval   0.5
        retry_check_interval    0.25
        max_check_attempts      3
}

# MAPREDUCE::TASKTRACKER Checks 
define service {
        hostgroup_name          tasktracker-servers
        use                     hadoop-service
        service_description     TASKTRACKER::TaskTracker process
        servicegroups           MAPREDUCE
        check_command           check_tcp_wrapper!{{ tasktracker_port }}!-w 1 -c 1
        _host_component         TASKTRACKER
        normal_check_interval   1
        retry_check_interval    0.5
        max_check_attempts      3
}

# MAPREDUCE::TASKTRACKER Mapreduce local dir used space
define service {
        hostgroup_name          tasktracker-servers
        use                     hadoop-service
        service_description     ::MapReduce local dir space
        servicegroups           MAPREDUCE
        check_command           check_mapred_local_dir_used_space!{{ mapred_local_dir }}!85%
        normal_check_interval   0.5
        retry_check_interval    0.25
        max_check_attempts      3
}

{% endif %}
{% endif %}


{% if hostgroup_defs['slaves'] %}
# HDFS::DATANODE Checks
define service {
        hostgroup_name          slaves
        use                     hadoop-service
        service_description     DATANODE::DataNode process
        servicegroups           HDFS
        check_command           check_tcp_wrapper!{{datanode_port}}!-w 1 -c 1
        _host_component         DATANODE
        normal_check_interval   1
        retry_check_interval    0.5
        max_check_attempts      3
}

define service {
        hostgroup_name          slaves
        use                     hadoop-service
        service_description     DATANODE::DataNode space
        servicegroups           HDFS
        check_command           check_datanode_storage!{{ datanode_port }}!90%!90%!{{ str(hadoop_ssl_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}!{{ str(security_enabled).lower() }}
        _host_component         DATANODE
        normal_check_interval   2
        retry_check_interval    1
        max_check_attempts      2
}

{% endif %}

{% if hostgroup_defs['flume-servers'] %}
# FLUME Checks
define service {
        hostgroup_name          flume-servers
        use                     hadoop-service
        service_description     FLUME::Flume Agent process
        servicegroups           FLUME
        check_command           check_tcp_wrapper!{{ flume_port }}!-w 1 -c 1
        _host_component         FLUME
        normal_check_interval   1
        retry_check_interval    0.5
        max_check_attempts      3
}
{% endif %}


{% if hostgroup_defs['zookeeper-servers'] %}
# ZOOKEEPER Checks
define service {
        hostgroup_name          zookeeper-servers
        use                     hadoop-service
        service_description     ZOOKEEPER::ZooKeeper Server process
        servicegroups           ZOOKEEPER
        check_command           check_tcp_wrapper!{{ clientPort }}!-w 1 -c 1
        _host_component         ZOOKEEPER_SERVER
        normal_check_interval   1
        retry_check_interval    0.5
        max_check_attempts      3
}
{% endif %}

{% if hostgroup_defs['hbasemasters'] %}
# HBASE::REGIONSERVER Checks
define service {
        hostgroup_name          region-servers
        use                     hadoop-service
        service_description     REGIONSERVER::RegionServer process
        servicegroups           HBASE
        check_command           check_tcp_wrapper!{{ hbase_rs_port }}!-w 1 -c 1
        _host_component         HBASE_REGIONSERVER
        normal_check_interval   1
        retry_check_interval    0.5
        max_check_attempts      3
}

{% if hostgroup_defs['hbasemasters'] %}
{% if check_cpu_on %}
define service {
        hostgroup_name          nagios-server
        use                     hadoop-service
        service_description     HBASEMASTER::HBase Master CPU utilization
        servicegroups           HBASE
        check_command           check_cpu_ha!{{ hbase_master_hosts_in_str }}!{{ hbase_master_port }}!200%!250%!{{ str(hadoop_ssl_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}!{{ str(security_enabled).lower() }}
        _host_component         HBASE_MASTER
        normal_check_interval   5
        retry_check_interval    2
        max_check_attempts      5
}
{%  endif %}
{%  endif %}

{%  for hbasemaster in hbase_master_hosts %}
define service {
        host_name               {{ hbasemaster }}
        use                     hadoop-service
        service_description     HBASEMASTER::HBase Master process on {{ hbasemaster }}
        servicegroups           HBASE
        check_command           check_tcp_wrapper!{{ hbase_master_rpc_port }}!-w 1 -c 1
        _host_component         HBASE_MASTER
        normal_check_interval   0.5
        retry_check_interval    0.25
        max_check_attempts      4
}
{% endfor %}
{% endif %}

{% if hostgroup_defs['hiveserver'] %}
# HIVE Metastore check
define service {
        hostgroup_name          hiveserver
        use                     hadoop-service
        service_description     HIVE-METASTORE::Hive Metastore process
        servicegroups           HIVE
        check_command           check_tcp_wrapper!{{ hive_metastore_port }}!-w 1 -c 1
        _host_component         HIVE_METASTORE
        normal_check_interval   0.5
        retry_check_interval    0.5
        max_check_attempts      3
}

# HIVE Server check
define service {
        hostgroup_name          hiveserver
        use                     hadoop-service
        service_description     HIVE-SERVER::HiveServer2 process
        servicegroups           HIVE
        check_command           check_tcp_wrapper_sasl!{{ hive_server_port }}!{{ hive_server2_authentication }}!{{ hive_server_principal }}!{{ kinitcmd }}
        _host_component         HIVE_SERVER
        normal_check_interval   0.5
        retry_check_interval    0.5
        max_check_attempts      3
}
{% endif %}
{% if hostgroup_defs['oozie-server'] %}
# Oozie check
define service {
        hostgroup_name          oozie-server
        use                     hadoop-service
        service_description     OOZIE::Oozie Server status
        servicegroups           OOZIE
        {% if security_enabled %}
        check_command           check_oozie_status!{{ oozie_server_port }}!{{ java64_home }}!true!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}
        {% else %}
        check_command           check_oozie_status!{{ oozie_server_port }}!{{ java64_home }}!false
        {% endif %}
        _host_component         OOZIE_SERVER
        normal_check_interval   1
        retry_check_interval    1
        max_check_attempts      3
}
{% endif %}
{% if hostgroup_defs['webhcat-server'] %}
# WEBHCAT check
define service {
        hostgroup_name          webhcat-server
        use                     hadoop-service
        service_description     WEBHCAT::WebHCat Server status
        servicegroups           HIVE
        {% if security_enabled %}
        check_command           check_templeton_status!{{ templeton_port }}!v1!{{ str(security_enabled).lower() }}!{{ nagios_keytab_path }}!{{ nagios_principal_name }}!{{ kinit_path_local }}
        {% else %}
        check_command           check_templeton_status!{{ templeton_port }}!v1!false
        {% endif %}
        _host_component         WEBHCAT_SERVER
        normal_check_interval   1
        retry_check_interval    0.5
        max_check_attempts      3
}
{% endif %}

{% if hostgroup_defs['hue-server'] %}
define service {
        hostgroup_name          hue-server
        use                     hadoop-service
        service_description     HUE::Hue Server status
        servicegroups           HUE
        check_command           check_hue_status
        _host_component         HUE
        normal_check_interval   100
        retry_check_interval    0.5
        max_check_attempts      3
}
{% endif %}

